﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Management;

namespace DownloadPlug_ins
{
    public partial class frmComputer : Form
    {
        public frmComputer()
        {
            InitializeComponent();
        }

        //获取主板序列号
        public string GetBIOSSerialNumber()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_BIOS");
                string sBIOSSerialNumber = "";
                foreach (ManagementObject mo in searcher.Get())
                {
                    sBIOSSerialNumber = mo["SerialNumber"].ToString().Trim();
                }
                return sBIOSSerialNumber;
            }
            catch
            {
                return "";
            }
        }
        //获取CPU序列号
        public string GetCPUSerialNumber()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Processor");
                string sCPUSerialNumber = "";
                foreach (ManagementObject mo in searcher.Get())
                {
                    sCPUSerialNumber = mo["ProcessorId"].ToString().Trim();
                }
                return sCPUSerialNumber;
            }
            catch
            {
                return "";
            }
        }
        //获取硬盘序列号
        public string GetHardDiskSerialNumber()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
                string sHardDiskSerialNumber = "";
                foreach (ManagementObject mo in searcher.Get())
                {
                    sHardDiskSerialNumber = mo["SerialNumber"].ToString().Trim();
                    break;
                }
                return sHardDiskSerialNumber;
            }
            catch
            {
                return "";
            }
        }
        //获取网卡地址
        public string GetNetCardMACAddress()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");
                string NetCardMACAddress = "";
                foreach (ManagementObject mo in searcher.Get())
                {
                    NetCardMACAddress = mo["MACAddress"].ToString().Trim();
                }
                return NetCardMACAddress;
            }
            catch
            {
                return "";
            }
        }

        private void gbtnOk_Click(object sender, EventArgs e)
        {
                string key2 = "greatwang_tony";

            string key = "SA_HZ_JQ"+"gt.wang";;
           // string text = "2B1B26A99E4C36322D1C3C75D659D50ED25F6EFD9BEEEC9B15B9470EF53B591DE2A86E7AE5E9B7E580D844E8B6C9E8446CF05E40CFBFBDF483B576AD334317071CE8CBCC1E94080266303E3A39810FDA2682788F0FC194E283A5AD2D3CDE50895942B6A3074936FEF1DD82C9F7712EA2749B27D4248C416525CFE217A62A09407E4A3837EA581252";
                           //2B1B26A99E4C36322D1C3C75D659D50ED25F6EFD9BEEEC9B15B9470EF53B591DE2A86E7AE5E9B7E580D844E8B6C9E8446CF05E40CFBFBDF483B576AD334317071CE8CBCC1E94080266303E3A39810FDA2682788F0FC194E283A5AD2D3CDE50895942B6A3074936FEF1DD82C9F7712EA2749B27D4248C416525CFE217A62A09407E4A3837EA581252
           // string path = @"E:\crm\Web\Config\License.ini";
            //string path2 = @"E:\crm\Web\";

            string path2 = cTextBox1.Text.Trim();
           // INI model = new INI(path);
           //string text = model.IniReadValue("License", "License1");
           // string text2 = model.IniReadValue("License", "License2");
           // string[] array = DESEncrypt.Decrypt(text, key).Split('@');


           // string a1 = DESEncrypt.Decrypt(array[0], key);

           // string a2 = DESEncrypt.Decrypt(array[1], key);
          //  string[] array2 = DESEncrypt.Decrypt(text2, key).Split('@');


            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            string path3 = "";
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                  path3 = folderBrowserDialog1.SelectedPath + @"\License.ini";
              
            }




            /////////加密

            string diskserialNumber = GetHardDiskSerialNumber();
            string cpuserialNumber = GetCPUSerialNumber();



            this.ctxtbox1.Text = diskserialNumber;
            this.cTextBox2.Text = cpuserialNumber;

            string value = diskserialNumber + cpuserialNumber + key;

            string value2 = path2.ToLower() + key;

            string b1 = DESEncrypt.Encrypt(value, key);
            string b2 = DESEncrypt.Encrypt(value2, key);
            string b3 = b1 + "@" + b2;






          //  string path3 = @"E:\crm\License.ini";
            INI newIni = new INI(path3);
            //[about]
            //string systemName = "#系统名称：CRM客户关系管理系统";
            //string company = "#研发公司：北京环球慧爽科技有限公司";
            //newIni.IniWriteValue("about", "", systemName);
            //newIni.IniWriteValue("about", "", company);
            //[crm]
            //string a1 = "#C3F0BEAD45A5D8C8";
            //string days = "#days=C6AC3ADDD059E114";
           // string a2 = "#C6B650C5B85FAF169B2C630BED63B0D6";
            string welcome = "6578255E5EC1C9869CDCF7E74E3E5E30";
            string last = "2065391691B5D34AC4F8828FF03FF9C1A57F5A577F6A3CA3";
            newIni.IniWriteValue("crm", "welcome", welcome);
            newIni.IniWriteValue("crm", "last", last);
            //[License]
            string b = DESEncrypt.Encrypt(b3, key);

            string license2 = "A017BB95D5A6188974CF3FB35A14567727C4624AE128041F19135AFB599AEC66";
            newIni.IniWriteValue("License", "License1", b);
            newIni.IniWriteValue("License", "License2", license2);

            MessageBox.Show("操作成功！");
        }
        
    }
}
